package com.japancompany.controller;

import com.japancompany.common.PageQuery;
import com.japancompany.common.PageResult;
import com.japancompany.common.Result;
import com.japancompany.entity.File;
import com.japancompany.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/files")
public class FileController {

    @Autowired
    private FileService fileService;

    @GetMapping("/page")
    public Result<PageResult<File>> getFilePage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String fileName,
            @RequestParam(required = false) String fileType) {
        
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(pageNum);
        pageQuery.setPageSize(pageSize);
        
        PageResult<File> result = fileService.getFilePage(pageQuery, fileName, fileType);
        return Result.success(result);
    }

    @GetMapping("/type/{fileType}")
    public Result<List<File>> getFilesByType(@PathVariable String fileType) {
        List<File> files = fileService.getFilesByType(fileType);
        return Result.success(files);
    }

    @GetMapping("/{id}")
    public Result<File> getFile(@PathVariable Long id) {
        File file = fileService.getById(id);
        if (file == null) {
            return Result.error("文件不存在");
        }
        return Result.success(file);
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> deleteFile(@PathVariable Long id) {
        boolean success = fileService.deleteFile(id);
        return Result.success(success);
    }
}